﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using LisBioGUIWPF.Controls.Project;
using LisBioGUIWPF.util;
using Entidades;
using DAO;
using System.Collections.Generic;
using LisBioGUIWPF.GuiFunctions;

namespace LisBioGUIWPF.Controls
{
    /// <summary>
    /// Lógica de interacción para ProyectoControl.xaml
    /// </summary>
    public partial class ProyectoControl : UserControl
    {
        
        private LisBio lisBio;
        private ProjectData.Operacion oper;

        public ProyectoControl(LisBio lisBio)
        {
            InitializeComponent();
            this.lisBio = lisBio;
            this.lblOperacion.Content = "";
        }

        private void btnUser_Click(object sender, RoutedEventArgs e)
        {
            this.gridBuscar.Visibility = Visibility.Hidden;
            this.lblOperacion.Content = "";

            ProjectData projectData = new ProjectData(lisBio, ProjectData.Operacion.NUEVO, this);
            projectData.txtNombre.Focus();
            this.IsEnabled = false;
            this.lisBio.borderHuella.Child = projectData;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (sender == this.btnDelete)
            {
                oper = ProjectData.Operacion.ELIMINAR;
                this.lblOperacion.Content = "Operación Eliminar";
            }
            else
            {
                oper = ProjectData.Operacion.MODIFICAR;
                this.lblOperacion.Content = "Operación Modificar";
            }

            this.gridBuscar.Visibility = Visibility.Visible;
            ImageControl image = new ImageControl(this.lisBio);
            image.setDefaultImage();
            this.lisBio.borderHuella.Child = image;

            CargarComponente.cargarProyectosCombo(comboBuscar);

            this.comboBuscar.Focus();
        }

        private void btnBuscar_Click(object sender, RoutedEventArgs e)
        {
            if (comboBuscar.SelectedItem != null)
            {
                ProjectData projectData = new ProjectData(lisBio, oper, this);
                projectData.txtNombre.Focus();

                projectData.Project = (EntidadProyecto)comboBuscar.SelectedItem;
                projectData.cargarDatos();

                this.IsEnabled = false;
                this.lisBio.borderHuella.Child = projectData;
            }
        }

        private void comboBuscar_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Escape &&
                e.Key != System.Windows.Input.Key.Down &&
                e.Key != System.Windows.Input.Key.Up &&
                e.Key != System.Windows.Input.Key.Left &&
                e.Key != System.Windows.Input.Key.Right &&
                e.Key != System.Windows.Input.Key.Enter)
            {

                this.comboBuscar.IsDropDownOpen = true;

                //COLOCAR EN LUGAR DE NULL LA LISTA QUE SE VA A FILTRAR
                //Util.FilterCombo(this.comboBuscar, null);
            }
            else if (e.Key == Key.Enter)
            {
                this.btnBuscar_Click(sender, null);
            }

            this.lblOperacion.Content = "";
        }

    }
}
